/****************************************************************************
 * 2025.8 小庆永不失眠
 ****************************************************************************/

using QAssetBundle;
using QFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

namespace ProjectSurvivor
{
	public partial class AcheievementPanel : UIElement,IController
	{
        ResLoader mResLoader = ResLoader.Allocate();
        private void Awake()
        {
            AchievementItemTemplate.Hide();

            var iconAltas = mResLoader.LoadSync<SpriteAtlas>("Icon");

            foreach (var item in this.GetSystem<AchievementSystem>().Items.OrderByDescending(item => item.Unlocked))
            {
                AchievementItemTemplate.InstantiateWithParent(AchievementItemRoot).Self((self) =>
                {

                    self.GetComponentInChildren<Text>().text = "<b>" + item.Name + (item.Unlocked ? "<color=green>[已完成]</color>" : "") + "</b>\n" + item.Description;
                    var sprite = iconAltas.GetSprite(item.IconName);
                    self.transform.Find("Icon").GetComponent<Image>().sprite = sprite;
                }).Show();
            }

            BtnClose.onClick.AddListener(() =>
            {
                AudioKit.PlaySound(Sfx.BUTTONCLICK);
                this.Hide();
            });
        }

        protected override void OnBeforeDestroy()
        {
            mResLoader.Recycle2Cache();
            mResLoader = null;
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}